package com.guigu.www.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.mapper.ApplyMapper;
import com.guigu.www.pojo.*;
import com.guigu.www.service.ApplyService;
import com.guigu.www.service.MManufactureService;
import com.guigu.www.service.imp.IdUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class MManufactureController {
    // 产品生产计划

    @Autowired
    ApplyService applyService;
    @Autowired
    MManufactureService mManufactureService;
    @Autowired
    IdUtils idUtils;


    //审核通过修改
    @RequestMapping("updateapplypojoshow.action")
    public Map updateapplypojoshow(@RequestBody Apply apply
    ) {
        Map map = new HashMap();
        apply.setManufacturetag("P001-1");
        boolean f = applyService.updateById(apply);
        if (f) {
            map.put("cm", "1");
        } else {
            map.put("cm", "0");
        }
        return map;
    }


    @RequestMapping("addmManufacture.action")
    public Map addmManufacture(@RequestBody MManufacture mManufacture
    ) {
        mManufacture.setManufactureId(idUtils.getid4(mManufacture));
        mManufacture.setCheckTag("S001-0");
        System.out.println(mManufacture);
        mManufacture.setManufactureProcedureTag("S002-0");
        mManufacture.setApplyIdGroup("  ");
        Map map = new HashMap();
        List<DesignProcedureDetails> list = mManufacture.getDetailss();
        for (DesignProcedureDetails details : list) {
            mManufacture.setId(null);
            mManufacture.setLabourCostPriceSum(details.getSubtotal());
            mManufacture.setModuleCostPriceSum(details.getModulesubtotal());
        }
        boolean f = mManufactureService.save(mManufacture);

        if (f) {
            map.put("msg", "制定成功,请复核");
            map.put("cm", "1");
        } else {
            map.put("msg", "制定失败");
        }

        return map;
    }

    //`adding`*
    @RequestMapping("readdmManufacture.action")
    public Map readdmManufacture(@RequestBody MManufacture mManufacture
    ) {
        Map map = new HashMap();
        mManufacture.setManufactureId(idUtils.getid4(mManufacture));
        mManufacture.setCheckTag("S001-2");
        mManufacture.setManufactureProcedureTag("S002-0");
        mManufacture.setApplyIdGroup(" ");
        List<DesignProcedureDetails> list = mManufacture.getDetailss();
        for (DesignProcedureDetails details : list) {
            mManufacture.setId(null);
            mManufacture.setLabourCostPriceSum(details.getSubtotal());
            mManufacture.setModuleCostPriceSum(details.getModulesubtotal());
        }
        boolean f = mManufactureService.save(mManufacture);
        if (f) {
            map.put("msg", "复核不通过，请重新制定");
            map.put("cm", "1");
        } else {
            map.put("msg", "失败");
        }

        return map;
    }




    //udpateapplypg//c
    //P001-0
    @RequestMapping("udpateapplypg.action")
    public void udpateapplypg(@RequestBody Apply apply
    ) {
        UpdateWrapper<Apply> updateWrapper=new UpdateWrapper<Apply>();
        updateWrapper.set("MANUFACTURE_TAG","P001-0").eq("PRODUCT_ID",apply.getProductid());
        boolean f= applyService.update(null,updateWrapper);
    }



    @RequestMapping("udpateMManufacture.action")
    public Map udpateMManufacture(@RequestBody MManufacture mManufacture
    ) {
        Map map = new HashMap();
        mManufacture.setCheckTag("S00-1");
        boolean f = mManufactureService.updateById(mManufacture);
        if (f) {
            map.put("msg", "审核成功");
        } else {
            map.put("msg", "审核失败");
        }
        return map;
    }

    //reshneghe
    @RequestMapping("reshnegheMManufacture.action")
    public Map reshnegheMManufacture(@RequestBody MManufacture mManufacture
    ) {
        Map map = new HashMap();
        mManufacture.setCheckTag("S00-3");
        boolean f = mManufactureService.updateById(mManufacture);
        if (f) {
            map.put("cm", "1");
        } else {
            map.put("cm", "0");
        }
        return map;
    }



    //审核
    @RequestMapping("queryMManufactureone.action")
    public PageVo<MManufacture> queryMManufactureone(MManufacture mManufacture,
                                                           @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                           @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
        QueryWrapper<MManufacture> queryWrapper = new QueryWrapper<MManufacture>();
        queryWrapper.eq("CHECK_TAG", "S001-0");

        Page<MManufacture> page = mManufactureService.page(new Page<MManufacture>(pagenum, pagesize), queryWrapper);

        PageVo<MManufacture> pageVo = new PageVo<MManufacture>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }

    //
    @RequestMapping("queryMManufactureoneall.action")
    public PageVo<MManufacture> queryMManufactureoneall(MManufacture mManufacture,
                                                           @RequestParam(value = "pagenum", defaultValue = "1") int pagenum,
                                                           @RequestParam(value = "pagesize", defaultValue = "5") int pagesize) {
        //条件查询
//        QueryWrapper<MManufacture> queryWrapper = new QueryWrapper<MManufacture>();
//        queryWrapper.eq("CHECK_TAG", "S001-0"); , queryWrapper

        Page<MManufacture> page = mManufactureService.page(new Page<MManufacture>(pagenum, pagesize));

        PageVo<MManufacture> pageVo = new PageVo<MManufacture>();
        pageVo.setRows(page.getRecords());
        pageVo.setTotal(page.getTotal());
        return pageVo;
    }
    //创建manufactureservice
    //根据查询所有派工审核通过的的派工单
    @RequestMapping("/queryByproducktionsregistrierung.action")
    public PageVo<MManufacture> queryByproducktionsregistrierung(@RequestParam(value = "pageno",defaultValue = "1") int pageno, @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        //创建pagevo
        PageVo<MManufacture> pageVo=new PageVo<MManufacture>();
        //获取方法
        IPage<MManufacture> iPage=mManufactureService.queryByproducktionsregistrierung(pageno,pagesize);
        //赋值
        pageVo.setTotal(iPage.getTotal());
        pageVo.setRows(iPage.getRecords());
        return  pageVo;
    }
    //根据当前生成的登记派工单号查询 工序
    @RequestMapping("/queryByMmanufacturebymanufactureId.action")
    public MManufacture queryByMmanufacturebymanufactureId(String manufactureId){
        //调用方法
        return mManufactureService.queryByMmanufacturebymanufactureId(manufactureId);
    }

    //查询所有等待审核的物料单
    @RequestMapping("/queryByMManufacturetag.action")
    public PageVo<MManufacture> queryByMManufacturetag(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                                       @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){

        //创建pageVo
        PageVo<MManufacture> pageVo=new PageVo<MManufacture>();
        //调用方法
       IPage<MManufacture> ipage=mManufactureService.queryByMManufacturetag(pageno,pagesize);
        //赋值
        pageVo.setRows(ipage.getRecords());
        pageVo.setTotal(ipage.getTotal());
        return  pageVo;

    }



    //查询总表所有的产品
    @RequestMapping("/queryBymmanfactureAll.action")
    public PageVo<MManufacture> queryBymmanfactureAll(@RequestParam(value = "pageno",defaultValue = "1") int pageno,
                                                      @RequestParam(value = "pagesize",defaultValue = "5") int pagesize){
        //创建pagevo
        PageVo<MManufacture> pageVo=new PageVo<MManufacture>();
        //调用方法
        IPage<MManufacture> iPage=mManufactureService.queryBymmanfactureAll(pageno,pagesize);
        //赋值
        pageVo.setTotal(iPage.getTotal());
        pageVo.setRows(iPage.getRecords());
        return pageVo;
    }



    //(序号)*
    @GetMapping("queryMManufacture2.action/{id}")
    public MManufacture queryMManufacture2(@PathVariable Integer id) {
        return mManufactureService.getById(id);
    }
}
